package com.yang.inventorymanagement.controllerview;


import com.yang.inventorymanagement.controllerview.suppliers.SuppliersManageSwing;
import com.yang.inventorymanagement.controllerview.users.UserLoginSwing;
import com.yang.inventorymanagement.pojo.User;
import com.yang.inventorymanagement.service.UserService;
import com.yang.inventorymanagement.utils.SessionUtils;
import com.yang.inventorymanagement.utils.SpringContextUtils;
import org.springframework.context.annotation.Lazy;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import javax.swing.*;
import javax.swing.border.EmptyBorder;

@Lazy
@Scope("prototype")
@Component("HomeManage")
public class HomeManage extends JFrame {


    JPanel contentPane;


    public HomeManage() {
        //获取登录用户
        User loginUser = (User) SessionUtils.getSession().get("user");
        //获取服务层
        UserService usersService = SpringContextUtils.getBean(UserService.class);

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 1200, 800);
        setLocationRelativeTo(null);
        setResizable(false);

        //菜单栏信息
        JMenuBar menuBar = new JMenuBar();
        setJMenuBar(menuBar);

        /**
         *用户个人中心的操作：修改个人信息，退出登录
         */
        JMenu adminMenu = new JMenu("欢迎，" + loginUser.getUsername());
        menuBar.add(adminMenu);
        //点击用户的时候修改信息
       /* JMenuItem updateAdminMenuItem = new JMenuItem("修改信息");
        adminMenu.add(updateAdminMenuItem);
        //修改用户信息
        updateAdminMenuItem.addActionListener((e) -> {
            ((UserUpdateSwing) SpringContextUtils.getBean("UserUpdateSwing")).setVisible(true);
        });*/

        JMenuItem logOutMenuItem = new JMenuItem("退出登陆");
        adminMenu.add(logOutMenuItem);
        //退出登陆
        logOutMenuItem.addActionListener((e) -> {
            if (usersService.loginOut()) {
                //隐藏本窗口
                setVisible(false);
                //打开新的窗口
                ((UserLoginSwing) SpringContextUtils.getBean("UserLoginSwing")).setVisible(true);
                //销毁窗口
                dispose();
            }
        });
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        JTabbedPane contentTablePane = new JTabbedPane(JTabbedPane.TOP);
        contentTablePane.setBounds(10, 10, 1170, 730);
        contentPane.add(contentTablePane);

        //添加默认页面
        JPanel indexPanel = new SuppliersManageSwing();
        contentTablePane.addTab("供应商管理", null, indexPanel, null);

        JPanel ProductsManageSwing = (JPanel) SpringContextUtils.getBean("ProductsManageSwing");
        contentTablePane.addTab("商品管理", null, ProductsManageSwing, null);


    }
}
